package state;

import java.util.Vector;

import particle.core.tools.PRandom;
import particle.core.view.ResourceSTLoader;
import particle.core.view.assets.Image;
import state.util.Hashtable;
import state.util.ResourceFinder;

public class ResourceManager extends ResourceSTLoader{
  public static final String EMPTY_CATEGORY = "empty";

  private static Hashtable imageByName = new Hashtable();

  private static Hashtable imageByCategory = new Hashtable();

  private static Vector loadResourceDoneListeners = new Vector();

  private static ResourceFinder resourceFinder = null;

  public static final int RESOURCE_IDLE_TIME = 5000;

  private static PRandom random = new PRandom(1000);

  public static void setResourceFinder(ResourceFinder resourceFinder1) {
    ResourceManager.resourceFinder = resourceFinder1;
  }

  public static void storeImage(GameImage image) {
    imageByName.put(image.getName(), image);

    if (image.getCategory() != null) {
      Hashtable categoryStore = getImageCategory(image.getCategory());
      categoryStore.put(image.getName(), image);
    }
  }

  public static void requestResource(String category, String name) {
    if (resourceFinder != null) {
      resourceFinder.requestResource(category, name);
    }
  }

  public static GameImage createEmptyImage() {
    GameImage image = new GameImage(null, EMPTY_CATEGORY, String.valueOf(System.currentTimeMillis()) + random.rand());
    storeImage(image);
    return image;
  }

  public static GameImage createImage(String category, String name) {
    GameImage image = new GameImage(null, category, name);
    storeImage(image);
    requestResource(category, name);
    return image;
  }

  public static Hashtable getImageCategory(String category) {
    Hashtable categoryStore = (Hashtable) imageByCategory.get(category);
    if (categoryStore == null) {
      categoryStore = new Hashtable();
      imageByCategory.put(category, categoryStore);
    }
    return categoryStore;
  }

  public static GameImage getImage(String category, String name) {
    GameImage image = null;
    if (category == null) {
      image = (GameImage) imageByName.get(name);
    } else {
      Hashtable categoryStore = getImageCategory(category);
      image = (GameImage) categoryStore.get(name);
    }

    if (image == null) {
      image = createImage(category, name);
    }
    return image;
  }

  public static void addLoadImageCompleteListener(LoadResourceCompleteListener listener) {
    loadResourceDoneListeners.addElement(listener);
  }
  
  public void loadImageFromJar(Image image, String name) {
    if (name.charAt(0) == '/') {
      name = "/res" + name;
    } else {
      name = "/res/" + name;
    }
    loadImage(image, name, false, true, -1, -1, -1, -1,-1, -1, name);
  }

  public static void updateImage(String category, String name, Image sourceImage) {
    if (name.endsWith("_320240.png")) {
      name = name.substring(0, name.indexOf("_320240.png")) + ".png";
    }

    // Update image
    GameImage image = getImage(category, name);
    image.setImage(sourceImage);

    // execute all the listener
    Vector completeListener = new Vector();
    for (int i = 0; i < loadResourceDoneListeners.size(); i++) {
      LoadResourceCompleteListener listener = (LoadResourceCompleteListener) loadResourceDoneListeners.elementAt(i);
      if (listener.getResourceName().equals(name)) {
        listener.onLoadResourceDone(image);
        completeListener.addElement(listener);
      }
    }

    // Remove complete listener
    for (int i = 0; i < completeListener.size(); i++) {
      loadResourceDoneListeners.removeElement(completeListener.elementAt(i));
    }
  }

  public static void removeImage(String category, String imageName) {
    imageByName.remove(imageName);
    if (category != null) {
      Hashtable getImageCategory = getImageCategory(category);
      if (getImageCategory != null) {
        getImageCategory.remove(imageName);
      }
    }
  }

  public static void clearIDLEResource() {
    Vector keys = imageByName.keys();
    for (int i = 0; i < keys.size(); i++) {
      GameImage image = (GameImage) imageByName.get(keys.elementAt(i));
      if (!image.isStatic() && (System.currentTimeMillis() - image.getLastTimeUse() > RESOURCE_IDLE_TIME)) {
        imageByName.remove(image.getName());
        
        if (image.getCategory() != null) {
          Hashtable getImageCategory = getImageCategory(image.getCategory());
          if (getImageCategory != null) {
            getImageCategory.remove(image);
          }
        }
        image.setImage(null);
      }
    }
  }
}
